<?php
include("include/conn.php");
require('include/Smarty/libs/Smarty.class.php');
$billBookId  = 0;
$partyId     = (isset($_POST['party'])) ? $_POST['party'] : 0;
$isEdit      = 0;
$billBookNo  = 1;
$msg         = isset($_GET['msg']) ? $$_GET['msg'] : "";
$queryField  = "";
$queryValue  = "";
$billDate    = "";
$rojmelEntry = "";
$party       = array();
$idOfRojmel  = array();
$billBookArr = array();
$totalFine   = (isset($_POST['totFine']) && $_POST['totFine'] > 0) ? $_POST['totFine'] : 0;
$totalAmount = (isset($_POST['totAmount']) && $_POST['totAmount'] > 0) ? $_POST['totAmount'] : 0;
$prevFine    = (isset($_POST['partyFine']) && $_POST['partyFine'] > 0) ? $_POST['partyFine'] : 0;
$prevAmount  = (isset($_POST['partyAmount']) && $_POST['partyAmount'] > 0) ? $_POST['partyAmount'] : 0;
$removePrev  = (isset($_POST['removePrev'])) ? 1 : 0;
$vepaariBookMasterId   = isset($_GET['vepaariBookMasterId']) ? $_GET['vepaariBookMasterId'] : "";
$requiredRojmelId      = 0 ;
$partyName      = "" ;
if(isset($_POST['billBookAddBtn']))
{
  if($_POST['isEdit'] = '1')
  {  
    if(isset($_GET['vepaariBookMasterId']) && $_GET['vepaariBookMasterId'] > 0)
    {
	    $updateBillBook = "UPDATE vepaaribillmaster 
	                          SET ".$queryField." partyId = ".$_POST['party'].", vepaariBookDate = '".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',
	                              totalFine = ".$totalFine.", totalAmount = ".$totalAmount.", prevFine = ".$prevFine.", prevAmount = ".$prevAmount."
	                        WHERE vepaariBookMasterId = ".$vepaariBookMasterId;
	    $updateBillBookRes = mysql_query($updateBillBook);
	    $billBookId = $vepaariBookMasterId;
	    
      $updateRojmelId = "SELECT rojmelId
                           FROM vepaaribilldetail 
                          WHERE vepaariBookMasterId = ".$vepaariBookMasterId;
      $updateRojmelIdRes = mysql_query($updateRojmelId);   
	    while($idRow = mysql_fetch_array($updateRojmelIdRes))
	    {
	    	$deleteRojmelQuery = "DELETE FROM rojmel
		                          WHERE rojmelId = ".$idRow['rojmelId'];
		    $deleteRojmelQueryRes = mysql_query($deleteRojmelQuery);
	    }
	    $deleteRojmelDetail = "DELETE FROM vepaaribilldetail
		                          WHERE vepaariBookMasterId = ".$vepaariBookMasterId;
		  $deleteRojmelDetailRes = mysql_query($deleteRojmelDetail);
		  
		  // Update master And Insert new records :Start
		  // Update master And Insert new records :End
	  }
	  else
	  {
	    // Master Entry :start
	    $billBookInsert = "INSERT INTO vepaaribillmaster(partyId,vepaariBookDate,prevFine,prevAmount)
	                       VALUES (".$_POST['party'].",'".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."',".$prevFine.",".$prevAmount.")";
	    $billBookInsertRes = mysql_query($billBookInsert);
	    $billBookId = mysql_insert_id();
	  }
	  
    $loopCount = 0;
    while($loopCount < count($_POST['particulars']))
    {
      $netWeight = ($_POST['netWeight'][$loopCount] != '') ? $_POST['netWeight'][$loopCount] : 0;
      $touch     = ($_POST['touch'][$loopCount] != '') ? $_POST['touch'][$loopCount] : 0;
      $waste     = ($_POST['waste'][$loopCount] != '') ? $_POST['waste'][$loopCount] : 0;
      $fine      = ($_POST['fine'][$loopCount] != '') ? $_POST['fine'][$loopCount] : 0;
      $qty       = ($_POST['qty'][$loopCount] != '') ? $_POST['qty'][$loopCount] : 0;
      $labour    = ($_POST['labour'][$loopCount] != '') ? $_POST['labour'][$loopCount] : 0;
      $amount    = ($_POST['amount'][$loopCount] != '') ? $_POST['amount'][$loopCount] : 0;
      
      if($_POST['particulars'][$loopCount] != ''|| $_POST['netWeight'][$loopCount] != ''|| $_POST['touch'][$loopCount] != ''|| $_POST['waste'][$loopCount] != ''|| $_POST['fine'][$loopCount] != ''|| $_POST['qty'][$loopCount] != ''|| $_POST['labour'][$loopCount] != ''|| $_POST['amount'][$loopCount] != '')
      {
      	$requiredRojmelId = 0;
      	/// Insert where checkbox selected :start
      	echo $_POST['rojmelEntry'][$loopCount]; 
        if(isset($_POST['rojmelEntry'][$loopCount]))
        {
          $getPartyName = "SELECT partyId,partyName 
                             FROM party
                            WHERE partyId =".$_POST['party'];
          $getPartyNameRes = mysql_query($getPartyName);
          if($partyNameRow = mysql_fetch_array($getPartyNameRes))
          {
		      	$partyName = $partyNameRow['partyName'];
		      	if($_POST['party'] > 0)
		      	$partyId   = $partyNameRow['partyId'];
		      	else
		      	$partyId   = 0;
		      }
		      
          $insertRoamel = "INSERT INTO rojmel(rojmelDate,vepaariPartyId,partyName,particulars, fine, amount, CrDr)
                           VALUES ('".$_POST['curDateYear']."-".$_POST['curDateMonth']."-".$_POST['curDateDay']."','".$partyId."',
                                   '".$partyName."','".$_POST['particulars'][$loopCount]."',".$fine.",".$amount.",'Dr')";
          $insertRoamelRes = mysql_query($insertRoamel);
          $requiredRojmelId = mysql_insert_id();          
        }/// Insert where checkbox selected :Ends
      	$billBookDetail = "INSERT INTO vepaaribilldetail(vepaariBookMasterId, particulars, netWeight, touch, waste,                                     fine, qty, labour,amount,CrDr,rojmelId,partyId)
                           VALUES (".$billBookId.",'".addslashes($_POST['particulars'][$loopCount])."',
                                   ".$netWeight.",".$touch.",".$waste.",".$fine.",
                                   ".$qty.",".$labour.",".$amount.",'Dr',".$requiredRojmelId.",".$_POST['party'].")";
        $billBookDetailRes = mysql_query($billBookDetail);
      }
      $loopCount++;
    }
  } 
  header("Location:vepaariBookListNew.php?party=$partyId&bdate=0");
}

if(isset($_GET['vepaariBookMasterId']) && $_GET['vepaariBookMasterId'] > 0)
{
  $isEdit = 1;
  
  $selectParty = "SELECT vepaariBookMasterId,partyId,vepaariBookDate,prevFine,prevAmount FROM vepaaribillmaster
                   WHERE vepaariBookMasterId = ".$vepaariBookMasterId;
  $selectPartyRes = mysql_query($selectParty);
  $row = mysql_fetch_assoc($selectPartyRes);
  $partyId    = $row['partyId'];
  $billDate   = $row['vepaariBookDate'];
  $prevFine   = $row['prevFine'];
  $prevAmount = $row['prevAmount'];
  
  $selectBill = "SELECT vepaariBookDetailId,vepaariBookMasterId,particulars, netWeight, touch, waste, fine, qty, labour, amount,rojmelId FROM vepaaribillDetail
                  WHERE vepaariBookMasterId = ".$vepaariBookMasterId;
  $selectBillRes = mysql_query($selectBill);
  $a = 0;
  while($billRow = mysql_fetch_assoc($selectBillRes))
  {
    $billBookArr[$a]['id']          = $billRow['vepaariBookDetailId'];
    $billBookArr[$a]['particulars'] = $billRow['particulars'];
    $billBookArr[$a]['netWeight']   = $billRow['netWeight'];
    $billBookArr[$a]['touch']       = $billRow['touch'];
    $billBookArr[$a]['waste']       = $billRow['waste'];
    $billBookArr[$a]['fine']        = $billRow['fine'];
    $billBookArr[$a]['qty']         = $billRow['qty'];
    $billBookArr[$a]['labour']      = $billRow['labour'];
    $billBookArr[$a]['amount']      = $billRow['amount'];
    $billBookArr[$a]['rojmelId']    = $billRow['rojmelId'];
    $a++;
  }
}

// This for select party : Start
$selectParty = "SELECT partyId,partyName 
                  FROM party
                 ORDER BY partyName DESC";
$selectPartyRes = mysql_query($selectParty);
$a = 0;
$party['partyId'][$a]   = 0;
$party['partyName'][$a] = "Select";
$a++;
while($partyRow = mysql_fetch_assoc($selectPartyRes))
{
  $party['partyId'][$a]   = $partyRow['partyId'];
  $party['partyName'][$a] = $partyRow['partyName'];
  $a++;
}

$smarty = new smarty;
require("bottom.php");

$smarty->assign("l_partySelect",$l_partySelect);
$smarty->assign("l_billBookDate",$l_billBookDate);
$smarty->assign("l_particulars",$l_particulars);
$smarty->assign("l_netWeight",$l_netWeight);
$smarty->assign("l_touch",$l_touch);
$smarty->assign("l_waste",$l_waste);
$smarty->assign("l_fine",$l_fine);
$smarty->assign("l_qty",$l_qty);
$smarty->assign("l_labour",$l_labour);
$smarty->assign("l_amount",$l_amount);
$smarty->assign("l_add",$l_add);
$smarty->assign("l_remove",$l_remove);
$smarty->assign("l_addBillBook",$l_addBillBook);
$smarty->assign("l_billBook",$l_billBook);
$smarty->assign("l_total",$l_total);
$smarty->assign("l_billBookDetailId",$l_billBookDetailId);
$smarty->assign("l_save",$l_save);

$smarty->assign("party",$party);
$smarty->assign("billBookNo",$billBookNo);
$smarty->assign("msg",$msg);
$smarty->assign("isEdit",$isEdit);
$smarty->assign("partyId",$partyId);
$smarty->assign("idOfRojmel",$idOfRojmel);
$smarty->assign("billDate",$billDate);
$smarty->assign("billBookArr",$billBookArr);
$smarty->assign("billBookId",$billBookId);
$smarty->assign("rojmelEntry",$rojmelEntry);
$smarty->assign("requiredRojmelId",$requiredRojmelId);
$smarty->assign("prevFine",$prevFine);
$smarty->assign("prevAmount",$prevAmount);
$smarty->assign("doSubmit",(isset($_POST['fromDateDay']) ? 0 : 1));
$smarty->display("vepaariBookJaavak.tpl");
?>